package com.bocai.dahuatech.ui.activity;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.utils.SharePrefencesUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AddAddressActivity extends BaseActivity implements View.OnClickListener
{
    @Bind(R.id.et_addr_add_name)
    EditText etName;
    @Bind(R.id.et_addr_add_phone)
    EditText etPhone;
    @Bind(R.id.et_addr_add_postcode)
    EditText etPostcode;
    @Bind(R.id.et_addr_add_road)
    EditText etRoad;
    @Bind(R.id.tv_addr_add_city)
    TextView tvCity;
    @Bind(R.id.ll_addr_add_city)
    LinearLayout llCity;
    String []cityIds;
    String []citys;

    @Override
    protected int contentView()
    {
        return R.layout.activity_add_address;
    }

    @Override
    protected void initView()
    {
        ButterKnife.bind(this);
        initToolbar(R.string.addaddrtitle);
        mToolbar.inflateMenu(R.menu.menu_addaddress_over);
        initEvent();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
        {
            @Override
            public boolean onMenuItemClick(MenuItem item)
            {
                //添加收货地址
                commitAddr();
                return false;
            }
        });
        llCity.setOnClickListener(this);
    }
    Observer<BaseBean> observer=new Observer<BaseBean>()
    {
        @Override
        public void onCompleted()
        {

        }


        @Override

        public void onError(Throwable e)
        {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error", e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean)
        {
            showToast(R.string.addaddrsuccese);
            Log.e("info",baseBean.getReturnInfo());
            AddAddressActivity.this.finish();
        }
    };

    @Override
    public void onEventMainThread(MainEvent event)
    {
        super.onEventMainThread(event);
        if(event.msg.equals("citychoseover"))
        {
            tvCity.setText(event.city);
            citys=event.city.split("-");
            cityIds=event.cityId.split("-");
        }

    }
    private void commitAddr()
    {
        if(TextUtils.isEmpty(etName.getText().toString()))
        {
            showToast(R.string.consigneenamenotnull);
        }else if (TextUtils.isEmpty(etPhone.getText().toString()))
        {
            showToast(R.string.phonenotnull);
        }
        else if (etPhone.getText().toString().length()!=11)
        {
            showToast(R.string.phonefalse);
        }
        else if (TextUtils.isEmpty(etPostcode.getText().toString()))
        {
            showToast(R.string.emailnotnull);
        }
        else if (etPostcode.getText().toString().length()!=6)
        {
            showToast(R.string.emailfalse);
        }
        else if (TextUtils.isEmpty(tvCity.getText().toString()))
        {
            showToast(R.string.citynolchoose);
        }else if (TextUtils.isEmpty(etRoad.getText().toString()))
        {
            showToast(R.string.addressnotnull);
        }else
        {
            getTestApi().editAddr(etName.getText().toString(),Integer.parseInt(cityIds[0]),Integer.parseInt(cityIds[1]),Integer.parseInt(cityIds[2]),
                    tvCity.getText().toString(),etRoad.getText().toString(),etPostcode.getText().toString(),
                    etPhone.getText().toString(), SharePrefencesUtil.getCustomerCode(this),1)
                    .compose(this.<BaseBean>bindToLifecycle())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        }

    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_addr_add_city:
                //选择省市区
                Intent intent=new Intent(this,ProviceListActivity.class);
                intent.putExtra("add",100);
                intent.putExtra("provice",SharePrefencesUtil.getProvince(this));
                intent.putExtra("proviceId",SharePrefencesUtil.getProvinceID(this));
                startActivity(intent);
                break;
        }
    }
}
